Generated by Cython 3.0.2
Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.
Raw output: noise_add_mixed_noise.c
+001: # cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=True
__pyx_t_4 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 1, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_4) < 0) __PYX_ERR(0, 1, __pyx_L1_error) __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
002: # code based on NanoJ-Core/Core/src/nanoj/core2/NanoJRandomNoise.java
003:
004: from libc.math cimport pow, log, sqrt, exp, pi, floor, fabs, fmax, fmin
005:
006: from ..utils.random cimport _random
007:
008: import cython
009: cimport cython
010: from cython.parallel import prange
011:
+012: import numpy as np
__pyx_t_7 = __Pyx_ImportDottedModule(__pyx_n_s_numpy, NULL); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 12, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_7); if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_7) < 0) __PYX_ERR(0, 12, __pyx_L1_error) __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
013: cimport numpy as np
014:
+015: r = np.random.RandomState()
__Pyx_GetModuleGlobalName(__pyx_t_7, __pyx_n_s_np); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 15, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_7); __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_7, __pyx_n_s_random); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 15, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; __pyx_t_7 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_RandomState); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 15, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_7); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_t_4 = __Pyx_PyObject_CallNoArg(__pyx_t_7); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 15, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; if (PyDict_SetItem(__pyx_d, __pyx_n_s_r, __pyx_t_4) < 0) __PYX_ERR(0, 15, __pyx_L1_error) __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
016:
+017: cdef double _log_factorial(int x) nogil:
static double __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__log_factorial(int __pyx_v_x) {
double __pyx_v_ans;
int __pyx_v_i;
double __pyx_v_y;
double __pyx_r;
/* … */
/* function exit code */
__pyx_L0:;
return __pyx_r;
}
018: """
019: Return the logarithm of the factorial of x. Uses Stirling's approximation for large values.
020: """
+021: cdef double ans = 0
__pyx_v_ans = 0.0;
+022: cdef int i = 0
__pyx_v_i = 0;
+023: cdef double y = x
__pyx_v_y = __pyx_v_x;
024:
+025: if x < 15:
__pyx_t_1 = (__pyx_v_x < 15);
if (__pyx_t_1) {
/* … */
}
+026: for i in range(1, x+1):
__pyx_t_2 = (__pyx_v_x + 1);
__pyx_t_3 = __pyx_t_2;
for (__pyx_t_4 = 1; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
__pyx_v_i = __pyx_t_4;
+027: ans += log(i)
__pyx_v_ans = (__pyx_v_ans + log(__pyx_v_i));
}
+028: return ans
__pyx_r = __pyx_v_ans;
goto __pyx_L0;
029: else:
+030: ans = y*log(y) + log(2.0*pi*y)/2 - y + (pow(y,-1))/12 - (pow(y,-3))/360 + (pow(y,-5))/1260 - (pow(y,-7))/1680 + (pow(y,-9))/1188
/*else*/ {
__pyx_v_ans = ((((((((__pyx_v_y * log(__pyx_v_y)) + (log(((2.0 * M_PI) * __pyx_v_y)) / 2.0)) - __pyx_v_y) + (pow(__pyx_v_y, -1.0) / 12.0)) - (pow(__pyx_v_y, -3.0) / 360.0)) + (pow(__pyx_v_y, -5.0) / 1260.0)) - (pow(__pyx_v_y, -7.0) / 1680.0)) + (pow(__pyx_v_y, -9.0) / 1188.0));
+031: return ans
__pyx_r = __pyx_v_ans;
goto __pyx_L0;
}
032:
+033: cdef int _poisson_small(double mean) nogil:
static int __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_small(double __pyx_v_mean) {
double __pyx_v_L;
double __pyx_v_p;
int __pyx_v_k;
int __pyx_r;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("_poisson_small", 1);
/* … */
/* function exit code */
__pyx_L1_error:;
#ifdef WITH_THREAD
__pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise._poisson_small", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = -1;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
__pyx_L0:;
__Pyx_RefNannyFinishContextNogil()
return __pyx_r;
}
034: """
035: Returns a poisson distributed random value with the specified mean using an algorithm based on the factorial function. This method is more efficient for small means.
036: """
+037: cdef double L = exp(-mean)
__pyx_v_L = exp((-__pyx_v_mean));
+038: cdef double p = 1.
__pyx_v_p = 1.;
+039: cdef int k = 1
__pyx_v_k = 1;
040:
+041: p *= _random()
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 41, __pyx_L1_error)
__pyx_v_p = (__pyx_v_p * __pyx_t_1);
042:
+043: while (p > L):
while (1) {
__pyx_t_2 = (__pyx_v_p > __pyx_v_L);
if (!__pyx_t_2) break;
+044: p *= _random()
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 44, __pyx_L1_error)
__pyx_v_p = (__pyx_v_p * __pyx_t_1);
+045: k += 1
__pyx_v_k = (__pyx_v_k + 1); }
046:
+047: return k - 1
__pyx_r = (__pyx_v_k - 1); goto __pyx_L0;
048:
049:
+050: cdef int _poisson_large(double mean) nogil:
static int __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_large(double __pyx_v_mean) {
double __pyx_v_c;
double __pyx_v_beta;
double __pyx_v_alpha;
double __pyx_v_k;
double __pyx_v_u;
double __pyx_v_x;
double __pyx_v_v;
double __pyx_v_y;
double __pyx_v_temp;
int __pyx_v_n;
double __pyx_v_lhs;
double __pyx_v_rhs;
int __pyx_r;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("_poisson_large", 1);
/* … */
/* function exit code */
__pyx_r = 0;
goto __pyx_L0;
__pyx_L1_error:;
#ifdef WITH_THREAD
__pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise._poisson_large", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = -1;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
__pyx_L0:;
__Pyx_RefNannyFinishContextNogil()
return __pyx_r;
}
051: """
052: Returns a poisson distributed random value with the specified mean using the rejection method PA. This method is more efficient for large means.
053: """
054: # "Rejection method PA" from "The Computer Generation of
055: # Poisson Random Variables" by A. C. Atkinson,
056: # Journal of the Royal Statistical Society Series C
057: # (Applied Statistics) Vol. 28, No. 1. (1979)
058: # The article is on pages 29-35.
059: # The algorithm given here is on page 32.
060:
+061: mean = fabs(mean)
__pyx_v_mean = fabs(__pyx_v_mean);
+062: cdef double c = 0.767 - 3.36/mean
__pyx_v_c = (0.767 - (3.36 / __pyx_v_mean));
+063: cdef double beta = pi/sqrt(3.0 * mean)
__pyx_v_beta = (((double)M_PI) / sqrt((3.0 * __pyx_v_mean)));
+064: cdef double alpha = beta*mean
__pyx_v_alpha = (__pyx_v_beta * __pyx_v_mean);
+065: cdef double k = log(c) - mean - log(beta)
__pyx_v_k = ((log(__pyx_v_c) - __pyx_v_mean) - log(__pyx_v_beta));
066:
067: cdef double u, x, v, y, temp
068: cdef int n
+069: while (True):
while (1) {
+070: u = _random()
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 70, __pyx_L1_error)
__pyx_v_u = __pyx_t_1;
+071: x = (alpha - log((1.0 - u) / u))/beta
__pyx_v_x = ((__pyx_v_alpha - log(((1.0 - __pyx_v_u) / __pyx_v_u))) / __pyx_v_beta);
+072: n = int(floor(x + 0.5))
__pyx_v_n = ((int)floor((__pyx_v_x + 0.5)));
+073: if n < 0:
__pyx_t_2 = (__pyx_v_n < 0);
if (__pyx_t_2) {
/* … */
}
+074: continue
goto __pyx_L3_continue;
+075: v = _random()
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 75, __pyx_L1_error)
__pyx_v_v = __pyx_t_1;
+076: y = alpha - beta*x
__pyx_v_y = (__pyx_v_alpha - (__pyx_v_beta * __pyx_v_x));
+077: temp = 1.0 + exp(y)
__pyx_v_temp = (1.0 + exp(__pyx_v_y));
+078: lhs = y + log(v / (temp * temp))
__pyx_v_lhs = (__pyx_v_y + log((__pyx_v_v / (__pyx_v_temp * __pyx_v_temp))));
+079: rhs = k + n*log(mean) - _log_factorial(n)
__pyx_t_1 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__log_factorial(__pyx_v_n); if (unlikely(__pyx_t_1 == ((double)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 79, __pyx_L1_error) __pyx_v_rhs = ((__pyx_v_k + (__pyx_v_n * log(__pyx_v_mean))) - __pyx_t_1);
+080: if lhs <= rhs:
__pyx_t_2 = (__pyx_v_lhs <= __pyx_v_rhs);
if (__pyx_t_2) {
/* … */
}
__pyx_L3_continue:;
}
+081: return n
__pyx_r = __pyx_v_n;
goto __pyx_L0;
082:
+083: cdef int _poisson_value(double mean) nogil:
static int __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_value(double __pyx_v_mean) {
int __pyx_r;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("_poisson_value", 1);
/* … */
/* function exit code */
__pyx_L1_error:;
#ifdef WITH_THREAD
__pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise._poisson_value", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = -1;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
__pyx_L0:;
__Pyx_RefNannyFinishContextNogil()
return __pyx_r;
}
084: """
085: Returns a poisson distributed random value with the specified mean. Uses a different algorithm for small and large means.
086: """
+087: if mean < 100:
__pyx_t_1 = (__pyx_v_mean < 100.0);
if (__pyx_t_1) {
/* … */
}
+088: return _poisson_small(mean)
__pyx_t_2 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_small(__pyx_v_mean); if (unlikely(__pyx_t_2 == ((int)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 88, __pyx_L1_error) __pyx_r = __pyx_t_2; goto __pyx_L0;
089: else:
+090: return _poisson_large(mean)
/*else*/ {
__pyx_t_2 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_large(__pyx_v_mean); if (unlikely(__pyx_t_2 == ((int)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 90, __pyx_L1_error)
__pyx_r = __pyx_t_2;
goto __pyx_L0;
}
091:
092:
+093: cdef double _normal_value() nogil:
static double __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__normal_value(void) {
double __pyx_v_u;
double __pyx_v_v;
double __pyx_v_r;
double __pyx_v_c;
double __pyx_r;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("_normal_value", 1);
/* … */
/* function exit code */
__pyx_L1_error:;
#ifdef WITH_THREAD
__pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise._normal_value", __pyx_clineno, __pyx_lineno, __pyx_filename);
__pyx_r = -1;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
__pyx_L0:;
__Pyx_RefNannyFinishContextNogil()
return __pyx_r;
}
094: """
095: Returns a normally distributed random value with mean 0 and standard deviation 1.
096: """
+097: cdef double u = _random() * 2 - 1
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 97, __pyx_L1_error)
__pyx_v_u = ((__pyx_t_1 * 2.0) - 1.0);
+098: cdef double v = _random() * 2 - 1
__pyx_t_1 = __pyx_f_7nanopyx_4core_5utils_6random__random(); if (unlikely(__Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 98, __pyx_L1_error)
__pyx_v_v = ((__pyx_t_1 * 2.0) - 1.0);
+099: cdef double r = u * u + v * v
__pyx_v_r = ((__pyx_v_u * __pyx_v_u) + (__pyx_v_v * __pyx_v_v));
+100: if r == 0 or r > 1:
__pyx_t_3 = (__pyx_v_r == 0.0);
if (!__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L4_bool_binop_done;
}
__pyx_t_3 = (__pyx_v_r > 1.0);
__pyx_t_2 = __pyx_t_3;
__pyx_L4_bool_binop_done:;
if (__pyx_t_2) {
/* … */
}
+101: return _normal_value()
__pyx_t_1 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__normal_value(); if (unlikely(__pyx_t_1 == ((double)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 101, __pyx_L1_error) __pyx_r = __pyx_t_1; goto __pyx_L0;
+102: cdef double c = sqrt(-2 * log(r) / r)
__pyx_v_c = sqrt(((-2.0 * log(__pyx_v_r)) / __pyx_v_r));
+103: return u * c
__pyx_r = (__pyx_v_u * __pyx_v_c); goto __pyx_L0;
104:
105:
+106: def add_mixed_gaussian_poisson_noise(image, double gauss_sigma, double gauss_mean):
/* Python wrapper */ static PyObject *__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_1add_mixed_gaussian_poisson_noise(PyObject *__pyx_self, #if CYTHON_METH_FASTCALL PyObject *const *__pyx_args, Py_ssize_t __pyx_nargs, PyObject *__pyx_kwds #else PyObject *__pyx_args, PyObject *__pyx_kwds #endif ); /*proto*/ PyDoc_STRVAR(__pyx_doc_7nanopyx_4core_8generate_21noise_add_mixed_noise_add_mixed_gaussian_poisson_noise, "\n Add mixed Gaussian-Poisson noise to an image, pure cython version\n :param image: The image to add noise to, need to be 2D or 3D\n :type image: numpy.ndarray or numpy.view\n :param gauss_sigma: The standard deviation of the Gaussian noise\n :type gauss_sigma: float\n :param gauss_mean: The mean of the Gaussian noise\n :type gauss_mean: float\n "); static PyMethodDef __pyx_mdef_7nanopyx_4core_8generate_21noise_add_mixed_noise_1add_mixed_gaussian_poisson_noise = {"add_mixed_gaussian_poisson_noise", (PyCFunction)(void*)(__Pyx_PyCFunction_FastCallWithKeywords)__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_1add_mixed_gaussian_poisson_noise, __Pyx_METH_FASTCALL|METH_KEYWORDS, __pyx_doc_7nanopyx_4core_8generate_21noise_add_mixed_noise_add_mixed_gaussian_poisson_noise}; static PyObject *__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_1add_mixed_gaussian_poisson_noise(PyObject *__pyx_self, #if CYTHON_METH_FASTCALL PyObject *const *__pyx_args, Py_ssize_t __pyx_nargs, PyObject *__pyx_kwds #else PyObject *__pyx_args, PyObject *__pyx_kwds #endif ) { PyObject *__pyx_v_image = 0; double __pyx_v_gauss_sigma; double __pyx_v_gauss_mean; #if !CYTHON_METH_FASTCALL CYTHON_UNUSED Py_ssize_t __pyx_nargs; #endif CYTHON_UNUSED PyObject *const *__pyx_kwvalues; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("add_mixed_gaussian_poisson_noise (wrapper)", 0); #if !CYTHON_METH_FASTCALL #if CYTHON_ASSUME_SAFE_MACROS __pyx_nargs = PyTuple_GET_SIZE(__pyx_args); #else __pyx_nargs = PyTuple_Size(__pyx_args); if (unlikely((__pyx_nargs < 0))) __PYX_ERR(0, 106, __pyx_L3_error) #endif #endif __pyx_kwvalues = __Pyx_KwValues_FASTCALL(__pyx_args, __pyx_nargs); { PyObject **__pyx_pyargnames[] = {&__pyx_n_s_image,&__pyx_n_s_gauss_sigma,&__pyx_n_s_gauss_mean,0}; PyObject* values[3] = {0,0,0}; if (__pyx_kwds) { Py_ssize_t kw_args; switch (__pyx_nargs) { case 3: values[2] = __Pyx_Arg_FASTCALL(__pyx_args, 2); CYTHON_FALLTHROUGH; case 2: values[1] = __Pyx_Arg_FASTCALL(__pyx_args, 1); CYTHON_FALLTHROUGH; case 1: values[0] = __Pyx_Arg_FASTCALL(__pyx_args, 0); CYTHON_FALLTHROUGH; case 0: break; default: goto __pyx_L5_argtuple_error; } kw_args = __Pyx_NumKwargs_FASTCALL(__pyx_kwds); switch (__pyx_nargs) { case 0: if (likely((values[0] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_image)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[0]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 106, __pyx_L3_error) else goto __pyx_L5_argtuple_error; CYTHON_FALLTHROUGH; case 1: if (likely((values[1] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_gauss_sigma)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[1]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 106, __pyx_L3_error) else { __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise", 1, 3, 3, 1); __PYX_ERR(0, 106, __pyx_L3_error) } CYTHON_FALLTHROUGH; case 2: if (likely((values[2] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_gauss_mean)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[2]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 106, __pyx_L3_error) else { __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise", 1, 3, 3, 2); __PYX_ERR(0, 106, __pyx_L3_error) } } if (unlikely(kw_args > 0)) { const Py_ssize_t kwd_pos_args = __pyx_nargs; if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_kwvalues, __pyx_pyargnames, 0, values + 0, kwd_pos_args, "add_mixed_gaussian_poisson_noise") < 0)) __PYX_ERR(0, 106, __pyx_L3_error) } } else if (unlikely(__pyx_nargs != 3)) { goto __pyx_L5_argtuple_error; } else { values[0] = __Pyx_Arg_FASTCALL(__pyx_args, 0); values[1] = __Pyx_Arg_FASTCALL(__pyx_args, 1); values[2] = __Pyx_Arg_FASTCALL(__pyx_args, 2); } __pyx_v_image = values[0]; __pyx_v_gauss_sigma = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_gauss_sigma == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 106, __pyx_L3_error) __pyx_v_gauss_mean = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_gauss_mean == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 106, __pyx_L3_error) } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise", 1, 3, 3, __pyx_nargs); __PYX_ERR(0, 106, __pyx_L3_error) goto __pyx_L3_error; __pyx_L3_error:; { Py_ssize_t __pyx_temp; for (__pyx_temp=0; __pyx_temp < (Py_ssize_t)(sizeof(values)/sizeof(values[0])); ++__pyx_temp) { __Pyx_Arg_XDECREF_FASTCALL(values[__pyx_temp]); } } __Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise.add_mixed_gaussian_poisson_noise", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; __pyx_r = __pyx_pf_7nanopyx_4core_8generate_21noise_add_mixed_noise_add_mixed_gaussian_poisson_noise(__pyx_self, __pyx_v_image, __pyx_v_gauss_sigma, __pyx_v_gauss_mean); int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; /* function exit code */ { Py_ssize_t __pyx_temp; for (__pyx_temp=0; __pyx_temp < (Py_ssize_t)(sizeof(values)/sizeof(values[0])); ++__pyx_temp) { __Pyx_Arg_XDECREF_FASTCALL(values[__pyx_temp]); } } __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_7nanopyx_4core_8generate_21noise_add_mixed_noise_add_mixed_gaussian_poisson_noise(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_image, double __pyx_v_gauss_sigma, double __pyx_v_gauss_mean) { float __pyx_v_v; int __pyx_v_i; int __pyx_v_j; int __pyx_v_f; __Pyx_memviewslice __pyx_v_image_2d = { 0, 0, { 0 }, { 0 }, { 0 } }; __Pyx_memviewslice __pyx_v_image_3d = { 0, 0, { 0 }, { 0 }, { 0 } }; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("add_mixed_gaussian_poisson_noise", 0); /* … */ /* function exit code */ __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_2); __PYX_XCLEAR_MEMVIEW(&__pyx_t_4, 1); __PYX_XCLEAR_MEMVIEW(&__pyx_t_15, 1); __Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise.add_mixed_gaussian_poisson_noise", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __PYX_XCLEAR_MEMVIEW(&__pyx_v_image_2d, 1); __PYX_XCLEAR_MEMVIEW(&__pyx_v_image_3d, 1); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* … */ __pyx_tuple__22 = PyTuple_Pack(9, __pyx_n_s_image, __pyx_n_s_gauss_sigma, __pyx_n_s_gauss_mean, __pyx_n_s_v, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_f, __pyx_n_s_image_2d, __pyx_n_s_image_3d); if (unlikely(!__pyx_tuple__22)) __PYX_ERR(0, 106, __pyx_L1_error) __Pyx_GOTREF(__pyx_tuple__22); __Pyx_GIVEREF(__pyx_tuple__22); /* … */ __pyx_t_4 = __Pyx_CyFunction_New(&__pyx_mdef_7nanopyx_4core_8generate_21noise_add_mixed_noise_1add_mixed_gaussian_poisson_noise, 0, __pyx_n_s_add_mixed_gaussian_poisson_noise, NULL, __pyx_n_s_nanopyx_core_generate_noise_add, __pyx_d, ((PyObject *)__pyx_codeobj__23)); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 106, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); if (PyDict_SetItem(__pyx_d, __pyx_n_s_add_mixed_gaussian_poisson_noise, __pyx_t_4) < 0) __PYX_ERR(0, 106, __pyx_L1_error) __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_codeobj__23 = (PyObject*)__Pyx_PyCode_New(3, 0, 0, 9, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__22, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_src_nanopyx_core_generate_noise, __pyx_n_s_add_mixed_gaussian_poisson_noise, 106, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__23)) __PYX_ERR(0, 106, __pyx_L1_error)
107: """
108: Add mixed Gaussian-Poisson noise to an image, pure cython version
109: :param image: The image to add noise to, need to be 2D or 3D
110: :type image: numpy.ndarray or numpy.view
111: :param gauss_sigma: The standard deviation of the Gaussian noise
112: :type gauss_sigma: float
113: :param gauss_mean: The mean of the Gaussian noise
114: :type gauss_mean: float
115: """
116:
+117: assert image.ndim == 2 or image.ndim == 3, "Only 2D and 3D images are supported"
#ifndef CYTHON_WITHOUT_ASSERTIONS
if (unlikely(__pyx_assertions_enabled())) {
__pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_image, __pyx_n_s_ndim); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 117, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_3 = (__Pyx_PyInt_BoolEqObjC(__pyx_t_2, __pyx_int_2, 2, 0)); if (unlikely((__pyx_t_3 < 0))) __PYX_ERR(0, 117, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
if (!__pyx_t_3) {
} else {
__pyx_t_1 = __pyx_t_3;
goto __pyx_L3_bool_binop_done;
}
__pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_image, __pyx_n_s_ndim); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 117, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_3 = (__Pyx_PyInt_BoolEqObjC(__pyx_t_2, __pyx_int_3, 3, 0)); if (unlikely((__pyx_t_3 < 0))) __PYX_ERR(0, 117, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_1 = __pyx_t_3;
__pyx_L3_bool_binop_done:;
if (unlikely(!__pyx_t_1)) {
__Pyx_Raise(__pyx_builtin_AssertionError, __pyx_kp_u_Only_2D_and_3D_images_are_suppor, 0, 0);
__PYX_ERR(0, 117, __pyx_L1_error)
}
}
#else
if ((1)); else __PYX_ERR(0, 117, __pyx_L1_error)
#endif
118:
119: cdef float v
120: cdef int i, j, f
121: cdef float[:,:] image_2d
122: cdef float[:,:,:] image_3d
123:
+124: if image.ndim == 2:
__pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_image, __pyx_n_s_ndim); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 124, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_t_1 = (__Pyx_PyInt_BoolEqObjC(__pyx_t_2, __pyx_int_2, 2, 0)); if (unlikely((__pyx_t_1 < 0))) __PYX_ERR(0, 124, __pyx_L1_error) __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; if (__pyx_t_1) { /* … */ goto __pyx_L5; }
+125: image_2d = image
__pyx_t_4 = __Pyx_PyObject_to_MemoryviewSlice_dsds_float(__pyx_v_image, PyBUF_WRITABLE); if (unlikely(!__pyx_t_4.memview)) __PYX_ERR(0, 125, __pyx_L1_error) __pyx_v_image_2d = __pyx_t_4; __pyx_t_4.memview = NULL; __pyx_t_4.data = NULL;
+126: with nogil:
{
#ifdef WITH_THREAD
PyThreadState *_save;
_save = NULL;
Py_UNBLOCK_THREADS
__Pyx_FastGIL_Remember();
#endif
/*try:*/ {
/* … */
/*finally:*/ {
/*normal exit:*/{
#ifdef WITH_THREAD
__Pyx_FastGIL_Forget();
Py_BLOCK_THREADS
#endif
goto __pyx_L8;
}
__pyx_L7_error: {
#ifdef WITH_THREAD
__Pyx_FastGIL_Forget();
Py_BLOCK_THREADS
#endif
goto __pyx_L1_error;
}
__pyx_L8:;
}
}
+127: for j in prange(image_2d.shape[0]):
if (unlikely(!__pyx_v_image_2d.memview)) { __Pyx_RaiseUnboundMemoryviewSliceNogil("image_2d"); __PYX_ERR(0, 127, __pyx_L7_error) }
__pyx_t_5 = (__pyx_v_image_2d.shape[0]);
{
#if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))
#undef likely
#undef unlikely
#define likely(x) (x)
#define unlikely(x) (x)
#endif
__pyx_t_7 = (__pyx_t_5 - 0 + 1 - 1/abs(1)) / 1;
if (__pyx_t_7 > 0)
{
#ifdef _OPENMP
#pragma omp parallel
#endif /* _OPENMP */
{
#ifdef _OPENMP
#pragma omp for lastprivate(__pyx_v_i) firstprivate(__pyx_v_j) lastprivate(__pyx_v_j) lastprivate(__pyx_v_v)
#endif /* _OPENMP */
for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_7; __pyx_t_6++){
{
__pyx_v_j = (int)(0 + 1 * __pyx_t_6);
/* Initialize private variables to invalid values */
__pyx_v_i = ((int)0xbad0bad0);
__pyx_v_v = ((float)__PYX_NAN());
+128: for i in range(image_2d.shape[1]):
__pyx_t_8 = (__pyx_v_image_2d.shape[1]);
__pyx_t_9 = __pyx_t_8;
for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {
__pyx_v_i = __pyx_t_10;
+129: v = image_2d[j,i]
__pyx_t_11 = __pyx_v_j;
__pyx_t_12 = __pyx_v_i;
__pyx_v_v = (*((float *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_image_2d.data + __pyx_t_11 * __pyx_v_image_2d.strides[0]) ) + __pyx_t_12 * __pyx_v_image_2d.strides[1]) )));
+130: v = _poisson_value(v) + _normal_value() * gauss_sigma + gauss_mean
__pyx_t_13 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_value(__pyx_v_v); if (unlikely(__pyx_t_13 == ((int)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 130, __pyx_L11_error) __pyx_t_14 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__normal_value(); if (unlikely(__pyx_t_14 == ((double)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 130, __pyx_L11_error) __pyx_v_v = ((__pyx_t_13 + (__pyx_t_14 * __pyx_v_gauss_sigma)) + __pyx_v_gauss_mean);
+131: v = fmax(v, 0)
__pyx_v_v = fmax(__pyx_v_v, 0.0);
+132: v = fmin(v, 65535)
__pyx_v_v = fmin(__pyx_v_v, 65535.0);
+133: image_2d[j,i] = v
__pyx_t_12 = __pyx_v_j;
__pyx_t_11 = __pyx_v_i;
*((float *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_image_2d.data + __pyx_t_12 * __pyx_v_image_2d.strides[0]) ) + __pyx_t_11 * __pyx_v_image_2d.strides[1]) )) = __pyx_v_v;
}
goto __pyx_L16;
__pyx_L11_error:;
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_exc_type)
#endif /* _OPENMP */
if (!__pyx_parallel_exc_type) {
__Pyx_ErrFetchWithState(&__pyx_parallel_exc_type, &__pyx_parallel_exc_value, &__pyx_parallel_exc_tb);
__pyx_parallel_filename = __pyx_filename; __pyx_parallel_lineno = __pyx_lineno; __pyx_parallel_clineno = __pyx_clineno;
__Pyx_GOTREF(__pyx_parallel_exc_type);
}
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
}
__pyx_parallel_why = 4;
goto __pyx_L15;
__pyx_L15:;
#ifdef _OPENMP
#pragma omp critical(__pyx_parallel_lastprivates1)
#endif /* _OPENMP */
{
__pyx_parallel_temp0 = __pyx_v_i;
__pyx_parallel_temp1 = __pyx_v_j;
__pyx_parallel_temp2 = __pyx_v_v;
}
__pyx_L16:;
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_why)
#endif /* _OPENMP */
}
}
#ifdef _OPENMP
Py_END_ALLOW_THREADS
#else
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
#endif /* _OPENMP */
/* Clean up any temporaries */
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
#ifndef _OPENMP
}
#endif /* _OPENMP */
}
}
if (__pyx_parallel_exc_type) {
/* This may have been overridden by a continue, break or return in another thread. Prefer the error. */
__pyx_parallel_why = 4;
}
if (__pyx_parallel_why) {
__pyx_v_i = __pyx_parallel_temp0;
__pyx_v_j = __pyx_parallel_temp1;
__pyx_v_v = __pyx_parallel_temp2;
switch (__pyx_parallel_why) {
case 4:
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_GIVEREF(__pyx_parallel_exc_type);
__Pyx_ErrRestoreWithState(__pyx_parallel_exc_type, __pyx_parallel_exc_value, __pyx_parallel_exc_tb);
__pyx_filename = __pyx_parallel_filename; __pyx_lineno = __pyx_parallel_lineno; __pyx_clineno = __pyx_parallel_clineno;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
}
goto __pyx_L7_error;
}
}
}
#if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))
#undef likely
#undef unlikely
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
}
134:
135: else:
+136: image_3d = image
/*else*/ {
__pyx_t_15 = __Pyx_PyObject_to_MemoryviewSlice_dsdsds_float(__pyx_v_image, PyBUF_WRITABLE); if (unlikely(!__pyx_t_15.memview)) __PYX_ERR(0, 136, __pyx_L1_error)
__pyx_v_image_3d = __pyx_t_15;
__pyx_t_15.memview = NULL;
__pyx_t_15.data = NULL;
+137: with nogil:
{
#ifdef WITH_THREAD
PyThreadState *_save;
_save = NULL;
Py_UNBLOCK_THREADS
__Pyx_FastGIL_Remember();
#endif
/*try:*/ {
/* … */
/*finally:*/ {
/*normal exit:*/{
#ifdef WITH_THREAD
__Pyx_FastGIL_Forget();
Py_BLOCK_THREADS
#endif
goto __pyx_L19;
}
__pyx_L18_error: {
#ifdef WITH_THREAD
__Pyx_FastGIL_Forget();
Py_BLOCK_THREADS
#endif
goto __pyx_L1_error;
}
__pyx_L19:;
}
}
}
__pyx_L5:;
+138: for f in prange(image_3d.shape[0]):
if (unlikely(!__pyx_v_image_3d.memview)) { __Pyx_RaiseUnboundMemoryviewSliceNogil("image_3d"); __PYX_ERR(0, 138, __pyx_L18_error) }
__pyx_t_7 = (__pyx_v_image_3d.shape[0]);
{
#if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))
#undef likely
#undef unlikely
#define likely(x) (x)
#define unlikely(x) (x)
#endif
__pyx_t_5 = (__pyx_t_7 - 0 + 1 - 1/abs(1)) / 1;
if (__pyx_t_5 > 0)
{
#ifdef _OPENMP
#pragma omp parallel
#endif /* _OPENMP */
{
#ifdef _OPENMP
#pragma omp for firstprivate(__pyx_v_f) lastprivate(__pyx_v_f) lastprivate(__pyx_v_i) lastprivate(__pyx_v_j) lastprivate(__pyx_v_v)
#endif /* _OPENMP */
for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6++){
{
__pyx_v_f = (int)(0 + 1 * __pyx_t_6);
/* Initialize private variables to invalid values */
__pyx_v_i = ((int)0xbad0bad0);
__pyx_v_j = ((int)0xbad0bad0);
__pyx_v_v = ((float)__PYX_NAN());
+139: for j in range(image_3d.shape[1]):
__pyx_t_8 = (__pyx_v_image_3d.shape[1]);
__pyx_t_9 = __pyx_t_8;
for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {
__pyx_v_j = __pyx_t_10;
+140: for i in range(image_3d.shape[2]):
__pyx_t_16 = (__pyx_v_image_3d.shape[2]);
__pyx_t_17 = __pyx_t_16;
for (__pyx_t_13 = 0; __pyx_t_13 < __pyx_t_17; __pyx_t_13+=1) {
__pyx_v_i = __pyx_t_13;
+141: v = image_3d[f,j,i]
__pyx_t_11 = __pyx_v_f;
__pyx_t_12 = __pyx_v_j;
__pyx_t_18 = __pyx_v_i;
__pyx_v_v = (*((float *) ( /* dim=2 */ (( /* dim=1 */ (( /* dim=0 */ (__pyx_v_image_3d.data + __pyx_t_11 * __pyx_v_image_3d.strides[0]) ) + __pyx_t_12 * __pyx_v_image_3d.strides[1]) ) + __pyx_t_18 * __pyx_v_image_3d.strides[2]) )));
+142: v = _poisson_value(v) + _normal_value() * gauss_sigma + gauss_mean
__pyx_t_19 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__poisson_value(__pyx_v_v); if (unlikely(__pyx_t_19 == ((int)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 142, __pyx_L22_error) __pyx_t_14 = __pyx_f_7nanopyx_4core_8generate_21noise_add_mixed_noise__normal_value(); if (unlikely(__pyx_t_14 == ((double)-1) && __Pyx_ErrOccurredWithGIL())) __PYX_ERR(0, 142, __pyx_L22_error) __pyx_v_v = ((__pyx_t_19 + (__pyx_t_14 * __pyx_v_gauss_sigma)) + __pyx_v_gauss_mean);
+143: v = fmax(v, 0)
__pyx_v_v = fmax(__pyx_v_v, 0.0);
+144: v = fmin(v, 65535)
__pyx_v_v = fmin(__pyx_v_v, 65535.0);
+145: image_3d[f,j,i] = v
__pyx_t_18 = __pyx_v_f;
__pyx_t_12 = __pyx_v_j;
__pyx_t_11 = __pyx_v_i;
*((float *) ( /* dim=2 */ (( /* dim=1 */ (( /* dim=0 */ (__pyx_v_image_3d.data + __pyx_t_18 * __pyx_v_image_3d.strides[0]) ) + __pyx_t_12 * __pyx_v_image_3d.strides[1]) ) + __pyx_t_11 * __pyx_v_image_3d.strides[2]) )) = __pyx_v_v;
}
}
goto __pyx_L29;
__pyx_L22_error:;
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_exc_type)
#endif /* _OPENMP */
if (!__pyx_parallel_exc_type) {
__Pyx_ErrFetchWithState(&__pyx_parallel_exc_type, &__pyx_parallel_exc_value, &__pyx_parallel_exc_tb);
__pyx_parallel_filename = __pyx_filename; __pyx_parallel_lineno = __pyx_lineno; __pyx_parallel_clineno = __pyx_clineno;
__Pyx_GOTREF(__pyx_parallel_exc_type);
}
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
}
__pyx_parallel_why = 4;
goto __pyx_L28;
__pyx_L28:;
#ifdef _OPENMP
#pragma omp critical(__pyx_parallel_lastprivates2)
#endif /* _OPENMP */
{
__pyx_parallel_temp0 = __pyx_v_f;
__pyx_parallel_temp1 = __pyx_v_i;
__pyx_parallel_temp2 = __pyx_v_j;
__pyx_parallel_temp3 = __pyx_v_v;
}
__pyx_L29:;
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_why)
#endif /* _OPENMP */
}
}
#ifdef _OPENMP
Py_END_ALLOW_THREADS
#else
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
#endif /* _OPENMP */
/* Clean up any temporaries */
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
#ifndef _OPENMP
}
#endif /* _OPENMP */
}
}
if (__pyx_parallel_exc_type) {
/* This may have been overridden by a continue, break or return in another thread. Prefer the error. */
__pyx_parallel_why = 4;
}
if (__pyx_parallel_why) {
__pyx_v_f = __pyx_parallel_temp0;
__pyx_v_i = __pyx_parallel_temp1;
__pyx_v_j = __pyx_parallel_temp2;
__pyx_v_v = __pyx_parallel_temp3;
switch (__pyx_parallel_why) {
case 4:
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = __Pyx_PyGILState_Ensure();
#endif
__Pyx_GIVEREF(__pyx_parallel_exc_type);
__Pyx_ErrRestoreWithState(__pyx_parallel_exc_type, __pyx_parallel_exc_value, __pyx_parallel_exc_tb);
__pyx_filename = __pyx_parallel_filename; __pyx_lineno = __pyx_parallel_lineno; __pyx_clineno = __pyx_parallel_clineno;
#ifdef WITH_THREAD
__Pyx_PyGILState_Release(__pyx_gilstate_save);
#endif
}
goto __pyx_L18_error;
}
}
}
#if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))
#undef likely
#undef unlikely
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
}
146:
147:
+148: def add_mixed_gaussian_poisson_noise2(np.ndarray image, double gauss_sigma, double gauss_mean):
/* Python wrapper */ static PyObject *__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_3add_mixed_gaussian_poisson_noise2(PyObject *__pyx_self, #if CYTHON_METH_FASTCALL PyObject *const *__pyx_args, Py_ssize_t __pyx_nargs, PyObject *__pyx_kwds #else PyObject *__pyx_args, PyObject *__pyx_kwds #endif ); /*proto*/ PyDoc_STRVAR(__pyx_doc_7nanopyx_4core_8generate_21noise_add_mixed_noise_2add_mixed_gaussian_poisson_noise2, "\n Add mixed Gaussian-Poisson noise to an image, pure numpy version\n :param image: The image to add noise to\n :type image: np.ndarray\n :param gauss_sigma: The standard deviation of the Gaussian noise\n :type gauss_sigma: float\n :param gauss_mean: The mean of the Gaussian noise\n :type gauss_mean: float\n "); static PyMethodDef __pyx_mdef_7nanopyx_4core_8generate_21noise_add_mixed_noise_3add_mixed_gaussian_poisson_noise2 = {"add_mixed_gaussian_poisson_noise2", (PyCFunction)(void*)(__Pyx_PyCFunction_FastCallWithKeywords)__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_3add_mixed_gaussian_poisson_noise2, __Pyx_METH_FASTCALL|METH_KEYWORDS, __pyx_doc_7nanopyx_4core_8generate_21noise_add_mixed_noise_2add_mixed_gaussian_poisson_noise2}; static PyObject *__pyx_pw_7nanopyx_4core_8generate_21noise_add_mixed_noise_3add_mixed_gaussian_poisson_noise2(PyObject *__pyx_self, #if CYTHON_METH_FASTCALL PyObject *const *__pyx_args, Py_ssize_t __pyx_nargs, PyObject *__pyx_kwds #else PyObject *__pyx_args, PyObject *__pyx_kwds #endif ) { PyArrayObject *__pyx_v_image = 0; double __pyx_v_gauss_sigma; double __pyx_v_gauss_mean; #if !CYTHON_METH_FASTCALL CYTHON_UNUSED Py_ssize_t __pyx_nargs; #endif CYTHON_UNUSED PyObject *const *__pyx_kwvalues; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("add_mixed_gaussian_poisson_noise2 (wrapper)", 0); #if !CYTHON_METH_FASTCALL #if CYTHON_ASSUME_SAFE_MACROS __pyx_nargs = PyTuple_GET_SIZE(__pyx_args); #else __pyx_nargs = PyTuple_Size(__pyx_args); if (unlikely((__pyx_nargs < 0))) __PYX_ERR(0, 148, __pyx_L3_error) #endif #endif __pyx_kwvalues = __Pyx_KwValues_FASTCALL(__pyx_args, __pyx_nargs); { PyObject **__pyx_pyargnames[] = {&__pyx_n_s_image,&__pyx_n_s_gauss_sigma,&__pyx_n_s_gauss_mean,0}; PyObject* values[3] = {0,0,0}; if (__pyx_kwds) { Py_ssize_t kw_args; switch (__pyx_nargs) { case 3: values[2] = __Pyx_Arg_FASTCALL(__pyx_args, 2); CYTHON_FALLTHROUGH; case 2: values[1] = __Pyx_Arg_FASTCALL(__pyx_args, 1); CYTHON_FALLTHROUGH; case 1: values[0] = __Pyx_Arg_FASTCALL(__pyx_args, 0); CYTHON_FALLTHROUGH; case 0: break; default: goto __pyx_L5_argtuple_error; } kw_args = __Pyx_NumKwargs_FASTCALL(__pyx_kwds); switch (__pyx_nargs) { case 0: if (likely((values[0] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_image)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[0]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 148, __pyx_L3_error) else goto __pyx_L5_argtuple_error; CYTHON_FALLTHROUGH; case 1: if (likely((values[1] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_gauss_sigma)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[1]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 148, __pyx_L3_error) else { __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise2", 1, 3, 3, 1); __PYX_ERR(0, 148, __pyx_L3_error) } CYTHON_FALLTHROUGH; case 2: if (likely((values[2] = __Pyx_GetKwValue_FASTCALL(__pyx_kwds, __pyx_kwvalues, __pyx_n_s_gauss_mean)) != 0)) { (void)__Pyx_Arg_NewRef_FASTCALL(values[2]); kw_args--; } else if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 148, __pyx_L3_error) else { __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise2", 1, 3, 3, 2); __PYX_ERR(0, 148, __pyx_L3_error) } } if (unlikely(kw_args > 0)) { const Py_ssize_t kwd_pos_args = __pyx_nargs; if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_kwvalues, __pyx_pyargnames, 0, values + 0, kwd_pos_args, "add_mixed_gaussian_poisson_noise2") < 0)) __PYX_ERR(0, 148, __pyx_L3_error) } } else if (unlikely(__pyx_nargs != 3)) { goto __pyx_L5_argtuple_error; } else { values[0] = __Pyx_Arg_FASTCALL(__pyx_args, 0); values[1] = __Pyx_Arg_FASTCALL(__pyx_args, 1); values[2] = __Pyx_Arg_FASTCALL(__pyx_args, 2); } __pyx_v_image = ((PyArrayObject *)values[0]); __pyx_v_gauss_sigma = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_gauss_sigma == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 148, __pyx_L3_error) __pyx_v_gauss_mean = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_gauss_mean == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 148, __pyx_L3_error) } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; __Pyx_RaiseArgtupleInvalid("add_mixed_gaussian_poisson_noise2", 1, 3, 3, __pyx_nargs); __PYX_ERR(0, 148, __pyx_L3_error) goto __pyx_L3_error; __pyx_L3_error:; { Py_ssize_t __pyx_temp; for (__pyx_temp=0; __pyx_temp < (Py_ssize_t)(sizeof(values)/sizeof(values[0])); ++__pyx_temp) { __Pyx_Arg_XDECREF_FASTCALL(values[__pyx_temp]); } } __Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise.add_mixed_gaussian_poisson_noise2", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_image), __pyx_ptype_5numpy_ndarray, 1, "image", 0))) __PYX_ERR(0, 148, __pyx_L1_error) __pyx_r = __pyx_pf_7nanopyx_4core_8generate_21noise_add_mixed_noise_2add_mixed_gaussian_poisson_noise2(__pyx_self, __pyx_v_image, __pyx_v_gauss_sigma, __pyx_v_gauss_mean); int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; /* function exit code */ goto __pyx_L0; __pyx_L1_error:; __pyx_r = NULL; __pyx_L0:; { Py_ssize_t __pyx_temp; for (__pyx_temp=0; __pyx_temp < (Py_ssize_t)(sizeof(values)/sizeof(values[0])); ++__pyx_temp) { __Pyx_Arg_XDECREF_FASTCALL(values[__pyx_temp]); } } __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_7nanopyx_4core_8generate_21noise_add_mixed_noise_2add_mixed_gaussian_poisson_noise2(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_image, double __pyx_v_gauss_sigma, double __pyx_v_gauss_mean) { PyObject *__pyx_v_shape = NULL; int __pyx_v_i; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("add_mixed_gaussian_poisson_noise2", 0); /* … */ /* function exit code */ __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); __Pyx_XDECREF(__pyx_t_7); __Pyx_XDECREF(__pyx_t_8); __Pyx_XDECREF(__pyx_t_9); __Pyx_XDECREF(__pyx_t_10); __Pyx_XDECREF(__pyx_t_11); __Pyx_AddTraceback("nanopyx.core.generate.noise_add_mixed_noise.add_mixed_gaussian_poisson_noise2", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XDECREF(__pyx_v_shape); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* … */ __pyx_tuple__24 = PyTuple_Pack(5, __pyx_n_s_image, __pyx_n_s_gauss_sigma, __pyx_n_s_gauss_mean, __pyx_n_s_shape, __pyx_n_s_i); if (unlikely(!__pyx_tuple__24)) __PYX_ERR(0, 148, __pyx_L1_error) __Pyx_GOTREF(__pyx_tuple__24); __Pyx_GIVEREF(__pyx_tuple__24); /* … */ __pyx_t_4 = __Pyx_CyFunction_New(&__pyx_mdef_7nanopyx_4core_8generate_21noise_add_mixed_noise_3add_mixed_gaussian_poisson_noise2, 0, __pyx_n_s_add_mixed_gaussian_poisson_noise_2, NULL, __pyx_n_s_nanopyx_core_generate_noise_add, __pyx_d, ((PyObject *)__pyx_codeobj__25)); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 148, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_4); if (PyDict_SetItem(__pyx_d, __pyx_n_s_add_mixed_gaussian_poisson_noise_2, __pyx_t_4) < 0) __PYX_ERR(0, 148, __pyx_L1_error) __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
149: """
150: Add mixed Gaussian-Poisson noise to an image, pure numpy version
151: :param image: The image to add noise to
152: :type image: np.ndarray
153: :param gauss_sigma: The standard deviation of the Gaussian noise
154: :type gauss_sigma: float
155: :param gauss_mean: The mean of the Gaussian noise
156: :type gauss_mean: float
157: """
+158: shape = []
__pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 158, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __pyx_v_shape = ((PyObject*)__pyx_t_1); __pyx_t_1 = 0;
+159: for i in range(image.ndim):
__pyx_t_2 = __pyx_f_5numpy_7ndarray_4ndim_ndim(__pyx_v_image); if (unlikely(__pyx_t_2 == ((int)-1) && PyErr_Occurred())) __PYX_ERR(0, 159, __pyx_L1_error) __pyx_t_3 = __pyx_t_2; for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) { __pyx_v_i = __pyx_t_4;
+160: shape.append(image.shape[i])
__pyx_t_5 = __pyx_f_5numpy_7ndarray_5shape_shape(__pyx_v_image); if (unlikely(__pyx_t_5 == ((npy_intp *)NULL) && PyErr_Occurred())) __PYX_ERR(0, 160, __pyx_L1_error) __pyx_t_1 = PyInt_FromSsize_t((__pyx_t_5[__pyx_v_i])); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 160, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __pyx_t_6 = __Pyx_PyList_Append(__pyx_v_shape, __pyx_t_1); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(0, 160, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; }
+161: image[:] = np.clip(r.poisson(image)+r.normal(scale=gauss_sigma, size=tuple(shape), loc=gauss_mean), 0, 65535)
__Pyx_GetModuleGlobalName(__pyx_t_7, __pyx_n_s_np); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_7); __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_7, __pyx_n_s_clip); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_8); __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; __Pyx_GetModuleGlobalName(__pyx_t_9, __pyx_n_s_r); if (unlikely(!__pyx_t_9)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_9); __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s_poisson); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_10); __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; __pyx_t_9 = NULL; __pyx_t_2 = 0; #if CYTHON_UNPACK_METHODS if (unlikely(PyMethod_Check(__pyx_t_10))) { __pyx_t_9 = PyMethod_GET_SELF(__pyx_t_10); if (likely(__pyx_t_9)) { PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_10); __Pyx_INCREF(__pyx_t_9); __Pyx_INCREF(function); __Pyx_DECREF_SET(__pyx_t_10, function); __pyx_t_2 = 1; } } #endif { PyObject *__pyx_callargs[2] = {__pyx_t_9, ((PyObject *)__pyx_v_image)}; __pyx_t_7 = __Pyx_PyObject_FastCall(__pyx_t_10, __pyx_callargs+1-__pyx_t_2, 1+__pyx_t_2); __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0; if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_7); __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; } __Pyx_GetModuleGlobalName(__pyx_t_10, __pyx_n_s_r); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_10); __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s_normal); if (unlikely(!__pyx_t_9)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_9); __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; __pyx_t_10 = __Pyx_PyDict_NewPresized(3); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_10); __pyx_t_11 = PyFloat_FromDouble(__pyx_v_gauss_sigma); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_11); if (PyDict_SetItem(__pyx_t_10, __pyx_n_s_scale, __pyx_t_11) < 0) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; __pyx_t_11 = PyList_AsTuple(__pyx_v_shape); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_11); if (PyDict_SetItem(__pyx_t_10, __pyx_n_s_size, __pyx_t_11) < 0) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; __pyx_t_11 = PyFloat_FromDouble(__pyx_v_gauss_mean); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_11); if (PyDict_SetItem(__pyx_t_10, __pyx_n_s_loc, __pyx_t_11) < 0) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_empty_tuple, __pyx_t_10); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_11); __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; __pyx_t_10 = PyNumber_Add(__pyx_t_7, __pyx_t_11); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_10); __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; __pyx_t_11 = NULL; __pyx_t_2 = 0; #if CYTHON_UNPACK_METHODS if (unlikely(PyMethod_Check(__pyx_t_8))) { __pyx_t_11 = PyMethod_GET_SELF(__pyx_t_8); if (likely(__pyx_t_11)) { PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_8); __Pyx_INCREF(__pyx_t_11); __Pyx_INCREF(function); __Pyx_DECREF_SET(__pyx_t_8, function); __pyx_t_2 = 1; } } #endif { PyObject *__pyx_callargs[4] = {__pyx_t_11, __pyx_t_10, __pyx_int_0, __pyx_int_65535}; __pyx_t_1 = __Pyx_PyObject_FastCall(__pyx_t_8, __pyx_callargs+1-__pyx_t_2, 3+__pyx_t_2); __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0; __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; } if (__Pyx_PyObject_SetSlice(((PyObject *)__pyx_v_image), __pyx_t_1, 0, 0, NULL, NULL, &__pyx_slice__5, 0, 0, 0) < 0) __PYX_ERR(0, 161, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;